package com.king.mvpdemo.newfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.king.mvpdemo.R;
import com.king.mvpdemo.base.BaseMvpFragment;
import com.king.mvpdemo.newfragment.adapter.RecyclerViewAdapter;
import com.king.mvpdemo.newfragment.bean.ListBean;
import com.king.mvpdemo.newfragment.contract.NewsContract;
import com.king.mvpdemo.newfragment.presenter.NewsPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author King
 * @Date 2019/8/8 15:39
 */
public class NewFragment extends BaseMvpFragment<NewsPresenter> implements NewsContract.View {

    @BindView(R.id.recycler_view_id)
    RecyclerView recyclerViewId;
    private String type;
    private String tys;

    private RecyclerViewAdapter recyclerViewAdapter;
    private List<ListBean.ResultBean.DataBean> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ButterKnife.bind(getActivity());
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.news_fragment;
    }

    @Override
    public void initView(View view) {
        mPresenter = new NewsPresenter();
        mPresenter.attachView(this);
        switch (type) {
            //类型,,top(头条，默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
            case "头条":
                tys = "top";
                break;
            case "社会":
                tys = "shehui";
                break;
            case "国内":
                tys = "guonei";
                break;
            case "国际":
                tys = "guoji";
                break;
            case "娱乐":
                tys = "yule";
                break;
            case "体育":
                tys = "tiyu";
                break;
            case "军事":
                tys = "junshi";
                break;
            case "科技":
                tys = "keji";
                break;
            case "财经":
                tys = "caijing";
                break;
            case "时尚":
                tys = "shishang";
                break;
        }
        // 定义一个线性布局管理器
        // 设置布局管理器
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        recyclerViewId.addItemDecoration(itemDecoration);
        recyclerViewId.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        mPresenter.getDate(tys, "edfb7d539e6e3d835479c3bf759504d5");

    }

    public Fragment newInstance(String type) {
        NewFragment fragment = new NewFragment();
        fragment.type = type;
        return fragment;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onSuccess(ListBean bean) {
        Toast.makeText(getActivity(), bean.getReason(), Toast.LENGTH_SHORT).show();
        Log.e("-----", bean.getResult().getData().get(0).getAuthor_name());
        list = bean.getResult().getData();

        // 设置adapter
        recyclerViewAdapter = new RecyclerViewAdapter(getContext(), list);
        recyclerViewId.setAdapter(recyclerViewAdapter);

    }



}
